package services

import (
	"github.com/spf13/cast"
	"goravel/app/models"
	"regexp"
	"strings"
)

type AdminRule struct {
}

// 前19个字符,日期时间
func (r *AdminRule) GetActionTime(str string) string {
	return str[:19]
}
func (r *AdminRule) HandleFields(str string, modelPrt *models.AdminLog) *models.AdminLog {
	//'76561199442781492:熊猫管理机器人(11)' Command: 'spawnitem Wooden_Plank  10  Location "< X=36458.848 Y=592017.750 Z=36171.559>"'
	//正则匹配
	action_time := r.GetActionTime(str)

	reg := regexp.MustCompile(`'(\d+):(.*)\((\d+)\)' Command: '(.*)'`)
	regClick := regexp.MustCompile(`'(\d+):(.*)\((\d+)\)' Used map click teleport to player: '(.*)' Location:\s+(.*)`)
	regTeleport := regexp.MustCompile(`'(\d+):(.*)\((\d+)\)' Target of TeleportTo: '(.*)' Location:\s+(.*)`)
	//Custom Zones updated by {服主徐迷儿 (13, 76561199081989219)}
	regCustom := regexp.MustCompile(`Custom Zones updated by {(.*)}`)
	match1 := reg.FindStringSubmatch(str)
	match2 := regClick.FindStringSubmatch(str)
	match3 := regTeleport.FindStringSubmatch(str)
	match4 := regCustom.FindStringSubmatch(str)
	if reg.MatchString(str) {
		//获取时间
		//获取用户ID
		user_id := match1[3]
		user_name := match1[2]
		stream_id := match1[1]
		//获取动作，动作原文
		command, OriginalText := r.GetCommandInfo(match1[4])
		modelPrt.ActionTime = action_time
		modelPrt.UserId = cast.ToInt(user_id)
		modelPrt.UserName = user_name
		modelPrt.StreamId = stream_id
		modelPrt.Command = command
		modelPrt.OriginalText = OriginalText
		return modelPrt
	}
	if regClick.MatchString(str) {
		//Used map click teleport to player
		//'76561199081989219:服主徐迷儿(1367)' Used map click teleport to player: '76561198433725158:猫宫(1409)' Location: X=-222090.172 Y=176782.344 Z=77404.828
		if regClick.MatchString(str) {
			//获取时间
			//获取用户ID
			user_id := match2[3]
			user_name := match2[2]
			stream_id := match2[1]
			modelPrt.ActionTime = action_time
			modelPrt.UserId = cast.ToInt(user_id)
			modelPrt.UserName = user_name
			modelPrt.StreamId = stream_id
			modelPrt.Command = "点击传送玩家"
			modelPrt.OriginalText = match2[4] + " " + match2[5]
			return modelPrt
		}
	}
	//	2023.11.24-12.36.46: '76561198134126064:管理员小兔(7)' Target of TeleportTo: '76561199520250880:自强(668)' Location: X=554667.188 Y=-409963.719 Z=2756.150
	if regTeleport.MatchString(str) {
		//获取时间
		//获取用户ID
		user_id := match3[3]
		user_name := match3[2]
		stream_id := match3[1]
		modelPrt.ActionTime = action_time
		modelPrt.UserId = cast.ToInt(user_id)
		modelPrt.UserName = user_name
		modelPrt.StreamId = stream_id
		modelPrt.Command = "传送到玩家"
		modelPrt.OriginalText = match3[4] + " " + match3[5]
		return modelPrt
	}

	if regCustom.MatchString(str) {
		//获取时间
		//获取用户ID
		user_name := match4[1]
		modelPrt.ActionTime = action_time
		modelPrt.UserId = 0
		modelPrt.UserName = user_name
		modelPrt.StreamId = ""
		modelPrt.Command = "更新领地"
		modelPrt.OriginalText = str
		return modelPrt
	}
	if modelPrt.Command == "" {
		modelPrt.Command = "未知"
		modelPrt.OriginalText = str
		return modelPrt
	}
	return nil
}

// 动作，动作原文
func (r *AdminRule) GetCommandInfo(str string) (string, string) {
	reg := regexp.MustCompile(`(\w)+\s`)
	command, str := "", str
	if reg.MatchString(str) {
		match := reg.FindStringSubmatch(str)
		/**
		spawnitem:刷物品
		Teleport：传送
		spawnvehicle：刷车
		SetGodMode：无敌
		SpawnInventoryFullOf：批量刷物品
		DestroyVehicle：摧毁车辆
		ChangeCurrencyBalance：改变货币余额
		SetFamePoints：改变声望点数
		*/
		if strings.Contains(match[0], "spawnitem") {
			command = "刷礼物"
		}
		if strings.Contains(match[0], "SpawnItem") {
			command = "刷礼物"
		}
		if strings.Contains(match[0], "Teleport") {
			command = "传送"
		}
		if strings.Contains(match[0], "teleport") {
			command = "传送"
		}
		if strings.Contains(match[0], "spawnzombie") {
			command = "刷僵尸"
		}

		if strings.Contains(match[0], "spawnvehicle") {
			command = "刷车"
		}
		if strings.Contains(match[0], "DestroyAllItemsWithinRadius") {
			command = "删除周围物品"
		}
		if strings.Contains(match[0], "SetGodMode") {
			command = "无敌"
		}
		if strings.Contains(match[0], "SpawnInventoryFullOf") {
			command = "批量刷物品"
		}
		if strings.Contains(match[0], "DestroyVehicle") {
			command = "摧毁车辆"
		}
		if strings.Contains(match[0], "ChangeCurrencyBalance") {
			command = "改变货币余额"
		}
		if strings.Contains(match[0], "SetFamePoints") {
			command = "改变声望点数"
		}
	} else {
		command = "未知"
	}
	return command, str
}
